<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script>
			function getUser(){
				//0..获取地址栏中查询字符串里的_uid
				//0.1吧查询字符串封装成一个对象
				var obj=new URLSearchParams(location.search)
				//0.2通过这个对象获取查询字符串中的参数
				var _uid=obj.get("uid")
				// var _uid=uid.value;			
				var xhr=new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4&&xhr.status==200){
						var r=xhr.responseText;
						var obj=JSON.parse(r)
						uname.value=obj.uname;
						email.value=obj.email;
						phone.value=obj.phone;
						user_name.value=obj.user_name;
						if(obj.gender==1){
							man.checked=true;
						}else if(obj.gender==0){
							woman.checked=true;
						}else{
							unk.checked=true;
						}
					}
				}
				xhr.open("get",`/pro/v1/search/${_uid}`,true)
				xhr.send();
			}
			
			function update(){
				//0..获取地址栏中查询字符串里的_uid
				//0.1吧查询字符串封装成一个对象
				var obj=new URLSearchParams(location.search)
				//0.2通过这个对象获取查询字符串中的参数
				var _uidobj=get("uid");
				var _uname=uname.value;
				var _email=email.value;
				var _phone=phone.value;
				var _user_name=user_name.value;
				var _gender="-1";//保密
				if(man.checked){
					_gender="1";
				}else if(woman.checked){
					_gender="0";
				}else{
					_gender="-1";
				}
				//1.创建xhr异步对象
				var xhr=new XMLHttpRequest();
				//4.接受响应
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4&&xhr.status==200){
						var r=xhr.responseText;
						if(r==1){
							alert("修改成功");
							location.href="list.html";
						}else{
							alert("操作错误")
						}
			    }
			}
			//3.创建请求
			//3.1创建请求主体
			var formdate=`uid=${_uid}&uname=${_uname}&email=${_email}&phone=${_phone}&user_name=${_user_name}&gender=${_gender}`;
			//3.2设置头信息
			xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			xhr.send(formdata);
		</script>
	</head>
	<body onload="getUser()">
		<!-- 用户的uid<input type="text"id="uid"><br> -->
		用户名称<input type="text"id="uname"><br>
		用户邮箱<input type="text"id="email"><br>
		用户方式<input type="text"id="phone"><br>
		用户姓名<input type="text"id="user_name"><br>
		<input type="radio"id="man"name="gender">男
		<input type="radio"id="woman"name="gender">女
		<input type="radio"id="unk"name="gender">保密
		<button onclick="updateUser()">修改</button><br>
	</body>
</html>
